package com.hddz.acquisition;

import android.util.Log;


import com.alibaba.fastjson.JSON;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.Map;

/**
 * Created by weiguo on 2017/10/16.
 */

public class XUtil {
    /**
     * 发送get请求
     *
     * @param <T>
     */
    public static <T> Callback.Cancelable Get(String url, Map<String, String> map, final Callback.CommonCallback<T> callback) {
        RequestParams params = new RequestParams(url);
        params.setReadTimeout(60000);
        if (null != map) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        Callback.Cancelable cancelable = x.http().get(params, callback);
        Log.d("HTTP", params.toString());
        return cancelable;
    }

    /**
     * 发送post请求
     *
     * @param <T>
     */
    public static <T> Callback.Cancelable Post(String url, Map<String, Object> map, Callback.CommonCallback<T> callback) {
        RequestParams params = new RequestParams(url);
        if (null != map) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    /**
     * 发送delete请求
     *
     * @param <T>
     */
    public static <T> Callback.Cancelable Delete(String url, Callback.CommonCallback<T> callback) {
        RequestParams params = new RequestParams(url);
        Callback.Cancelable cancelable = x.http().request(HttpMethod.DELETE, params, callback);
        return cancelable;
    }

    public static <T> Callback.Cancelable PostJson(String url, String json, Callback.CommonCallback<T> callback) {
        Log.d("HTTP", url + " : " + json);
        RequestParams params = new RequestParams(url);
       params.setAsJsonContent(true);
        if (null != json) {
            params.setBodyContent(json);
        }
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    public static <T> Callback.Cancelable PostJson(String url, Object json, Callback.CommonCallback<T> callback) {
        Log.d("HTTP", url + " : " + json);
        RequestParams params = new RequestParams(url);
        params.setReadTimeout(30000);
        params.setAsJsonContent(true);
        if (null != json) {
            params.setBodyContent(JSON.toJSONString(json) );
        }
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    public static <T> Callback.Cancelable PutJson(String url, String json, Callback.CommonCallback<T> callback) {
        Log.d("HTTP", url + " : " + json);
        RequestParams params = new RequestParams(url);
        params.setAsJsonContent(true);
        if (null != json) {
            params.setBodyContent(json);
        }
        Callback.Cancelable cancelable = x.http().request(HttpMethod.PUT, params, callback);
        return cancelable;
    }

    /**
     * 上传文件
     *
     * @param <T>
     */
    public static <T> Callback.Cancelable UpLoadFile(String url, Map<String, Object> map, Callback.CommonCallback<T> callback) {
        RequestParams params = new RequestParams(url);
        if (null != map) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    public static <T> Callback.Cancelable UpLoadFile(String url, File file, Callback.CommonCallback<T> callback) {
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("upload", file);
        params.setMultipart(true);
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    /**
     * 下载文件
     *
     * @param <T>
     */
    public static <T> Callback.Cancelable DownLoadFile(String url, String filepath, Callback.CommonCallback<T> callback) {
        RequestParams params = new RequestParams(url);
       //设置断点续传
        params.setAutoResume(true);
        params.setSaveFilePath(filepath);
        Callback.Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }
}
